<template>
    <el-dialog
        title="测试添加菜单"
        :visible.sync="showValue"
        append-to-body
        :close-on-click-modal="false"
        width="450px"
        :before-close="clearData"
    >
        <div class="wrap">
            <el-form ref="form" :model="form" size="small" label-width="120px">
                <el-form-item label="菜单图标" prop="icon">
                    <el-input class="formItem" v-model="form.icon"></el-input>
                </el-form-item>
                <el-form-item label="菜单名称" prop="menuName">
                    <el-input
                        class="formItem"
                        v-model="form.menuName"
                    ></el-input>
                </el-form-item>
                <el-form-item label="菜单代码" prop="menucode">
                    <el-input
                        class="formItem"
                        v-model="form.menucode"
                    ></el-input>
                </el-form-item>
                <el-form-item label="父菜单代码" prop="parentMencode">
                    <el-input
                        class="formItem"
                        v-model="form.parentMencode"
                    ></el-input>
                </el-form-item>
                <el-form-item label="菜单排序" prop="sort">
                    <el-input class="formItem" v-model="form.sort"></el-input>
                </el-form-item>
            </el-form>
        </div>
        <span slot="footer">
            <el-button size="small" @click="clearData">取 消</el-button>
            <el-button
                type="primary"
                size="small"
                :loading="loading"
                @click="save"
                >确 定</el-button
            >
        </span>
    </el-dialog>
</template>

<script>
export default {
    props: {
        value: Boolean
    },
    data () {
        return {
            loading: false,
            showValue: false,
            form: {
                icon: '',
                menuName: '',
                menuState: 1,
                menucode: '',
                parentMencode: '',
                sort: ''
            }
        }
    },
    watch: {
        value (val) {
            console.log(val)
            this.showValue = val
        },
        showValue (val) {
            this.$emit('update:value', val)
        }
    },
    methods: {
        // 清空数据
        clearData () {
            this.loading = false
            this.showValue = false
        },
        // 保存表单
        save () {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.loading = true
                    console.log(this.form)
                    this.$http.post(this.$store.state.IP + '/systemMenuManage/addMenu', {
                        ...this.form
                    },
                        {
                            'emulateJSON': false
                        }
                    ).then(response => {
                        console.log('-------------------------------------------')
                        response = response.body
                        console.log(response)
                        if (response.ret === '0') {
                            this.$message({
                                showClose: true,
                                message: '添加成功',
                                type: 'success'
                            })
                        }
                         this.loading = false
                        if (response.ret !== '0') {
                            console.log('添加失败')
                        }
                        console.log('******************************************')
                    }, response => {
                        console.log('调用添加失败')
                    })
                } else {
                    console.log('error submit!!')
                    return false
                }
            })
        }
    }
}
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
.formItem
    width: 220px
</style>